package com.example.mybatis.Service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatis.Pojo.Score;
import com.example.mybatis.Service.ScoreService;
import com.example.mybatis.mapper.ScoreMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score> implements ScoreService {

    //通过学号和开课学期来查询数据
    public List<Score> findByUsernameAndKxxq(String username, String kkxq) {
        QueryWrapper<Score> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        if (!StringUtils.isEmpty(kkxq)) {
            wrapper.eq("kkxq", kkxq);
        }
        return list(wrapper);
    }

    public List<Score> selectScoresByUsername(String username) {
        QueryWrapper<Score> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        return list(wrapper);
    }

    public Map<String, Double> selectCreditsByNature(String username) {
        List<Score> scores = selectScoresByUsername(username);
        Map<String, Double> credits = new HashMap<>();
        for (Score score : scores) {
            String nature = score.getKcxz();
            Double grade = Double.valueOf(score.getCj());
            if (grade >= 60) {
                Double credit = Double.valueOf(score.getXf());
                if (credits.containsKey(nature)) {
                    credits.put(nature, credits.get(nature) + credit);
                } else {
                    credits.put(nature, credit);
                }
            }
        }
        return credits;
    }

}
